#include <bits/stdc++.h>
using namespace std;

using ll = long long;

void solve()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n >> m;
        vector<int> x(n), r(n);
        for (int i = 0; i < n; ++i)
            cin >> x[i];
        for (int i = 0; i < n; ++i)
            cin >> r[i];

        vector<pair<int, int>> cr;
        for (int i = 0; i < n; ++i)
            cr.emplace_back(r[i], x[i]);
        sort(cr.begin(), cr.end(), greater<pair<int, int>>());

        vector<int> sr, sx;
        for (auto &c : cr)
        {
            sr.push_back(c.first);
            sx.push_back(c.second);
        }

        int Rm = sr.empty() ? 0 : sr[0];
        ll tot = 0;

        vector<pair<int, int>> iv;
        for (int i = 0; i < n; ++i)
        {
            int ri = sr[i], xi = sx[i];
            iv.emplace_back(xi - ri, xi + ri);
        }
        sort(iv.begin(), iv.end());
        vector<pair<int, int>> miv;
        for (auto &i : iv)
        {
            if (miv.empty())
                miv.push_back(i);
            else
            {
                auto &lst = miv.back();
                if (i.first <= lst.second + 1)
                    lst.second = max(lst.second, i.second);
                else
                    miv.push_back(i);
            }
        }
        for (auto [l, r] : miv)
            tot += r - l + 1;

        for (int y = 1; y <= Rm; ++y)
        {
            int low = 0, high = n - 1, j = -1;
            while (low <= high)
            {
                int mid = (low + high) / 2;
                if (sr[mid] >= y)
                {
                    j = mid;
                    low = mid + 1;
                }
                else
                    high = mid - 1;
            }
            if (j == -1)
                continue;

            vector<pair<int, int>> yv;
            for (int i = 0; i <= j; ++i)
            {
                int ri = sr[i], xi = sx[i];
                int ki = sqrt(ri * ri - y * y);
                yv.emplace_back(xi - ki, xi + ki);
            }

            sort(yv.begin(), yv.end());
            miv.clear();
            for (auto &i : yv)
            {
                if (miv.empty())
                    miv.push_back(i);
                else
                {
                    auto &lst = miv.back();
                    if (i.first <= lst.second + 1)
                        lst.second = max(lst.second, i.second);
                    else
                        miv.push_back(i);
                }
            }

            ll cnt = 0;
            for (auto [l, r] : miv)
                cnt += r - l + 1;
            tot += cnt * 2;
        }

        cout << tot << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}